package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dto.SimplePropsDTO;
import com.sc.nft.entity.SecondEquityPropsConfig;
import com.sc.nft.entity.vo.AdminSecondEquityPropsPageVO;
import com.sc.nft.mapper.SecondEquityPropsConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 二级-权益道具配置表(sc_second_equity_props_config)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-04-07 14:18:15
 */
@Slf4j
@Repository
public class SecondEquityPropsConfigDao extends ServiceImpl<SecondEquityPropsConfigMapper, SecondEquityPropsConfig> {

    public Page<AdminSecondEquityPropsPageVO> getPage(String firstTitle, Page page) {
        return baseMapper.getPage(page, firstTitle);
    }

    public boolean getIsExistByEquityPropsId(Long equityPropsId) {
        LambdaQueryWrapper<SecondEquityPropsConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondEquityPropsConfig::getEquityPropsId, equityPropsId);
        return count(wrapper) > 0;
    }

    public Boolean editSecondCollection(SecondEquityPropsConfig secondEquityPropsConfig) {
        LambdaUpdateWrapper<SecondEquityPropsConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(ObjectUtil.isNotNull(secondEquityPropsConfig.getMinPrice()), SecondEquityPropsConfig::getMinPrice, secondEquityPropsConfig.getMinPrice());
        wrapper.set(ObjectUtil.isNotNull(secondEquityPropsConfig.getMaxPrice()), SecondEquityPropsConfig::getMaxPrice, secondEquityPropsConfig.getMaxPrice());
        wrapper.set(ObjectUtil.isNotNull(secondEquityPropsConfig.getCharge()), SecondEquityPropsConfig::getCharge, secondEquityPropsConfig.getCharge());
        wrapper.set(ObjectUtil.isNotNull(secondEquityPropsConfig.getMinCharge()), SecondEquityPropsConfig::getMinCharge, secondEquityPropsConfig.getMinCharge());
        wrapper.set(ObjectUtil.isNotNull(secondEquityPropsConfig.getPlatformShow()), SecondEquityPropsConfig::getPlatformShow, secondEquityPropsConfig.getPlatformShow());
        wrapper.set(ObjectUtil.isNotNull(secondEquityPropsConfig.getBrandShow()), SecondEquityPropsConfig::getBrandShow, secondEquityPropsConfig.getBrandShow());
        wrapper.set(ObjectUtil.isNotNull(secondEquityPropsConfig.getSort()), SecondEquityPropsConfig::getSort, secondEquityPropsConfig.getSort());
        wrapper.set(SecondEquityPropsConfig::getPanelId, secondEquityPropsConfig.getPanelId());
        wrapper.set(SecondEquityPropsConfig::getEnableWantBuy, secondEquityPropsConfig.getEnableWantBuy());
        wrapper.set(SecondEquityPropsConfig::getMinWantBuyPrice, secondEquityPropsConfig.getMinWantBuyPrice());
        wrapper.set(SecondEquityPropsConfig::getMaxWantBuyPrice, secondEquityPropsConfig.getMaxWantBuyPrice());
        wrapper.set(SecondEquityPropsConfig::getWantBuyQuantity, secondEquityPropsConfig.getWantBuyQuantity());
        wrapper.set(SecondEquityPropsConfig::getEnableWantBuyHoldCondition, secondEquityPropsConfig.getEnableWantBuyHoldCondition());
        wrapper.set(SecondEquityPropsConfig::getEnableWantBuyConsumeProp, secondEquityPropsConfig.getEnableWantBuyConsumeProp());
        wrapper.set(SecondEquityPropsConfig::getWantBuyHoldCollectionId, secondEquityPropsConfig.getWantBuyHoldCollectionId());
        wrapper.set(SecondEquityPropsConfig::getWantBuyConsumePropId, secondEquityPropsConfig.getWantBuyConsumePropId());
        wrapper.set(SecondEquityPropsConfig::getWantBuyConsumePropQuantity, secondEquityPropsConfig.getWantBuyConsumePropQuantity());
        wrapper.eq(SecondEquityPropsConfig::getId, secondEquityPropsConfig.getId());
        return update(wrapper);
    }

    public boolean updateStatusById(Long id, Boolean status) {
        LambdaUpdateWrapper<SecondEquityPropsConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(SecondEquityPropsConfig::getStatus, status);
        wrapper.eq(SecondEquityPropsConfig::getId, id);
        return update(wrapper);
    }

    public boolean updateSortById(Long id, Integer sort) {
        LambdaUpdateWrapper<SecondEquityPropsConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(SecondEquityPropsConfig::getSort, sort);
        wrapper.eq(SecondEquityPropsConfig::getId, id);
        return update(wrapper);
    }

    public List<Long> getIdsByStatus(Boolean status) {
        LambdaQueryWrapper<SecondEquityPropsConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondEquityPropsConfig::getStatus, status);
        return list(wrapper).stream()
                .map(SecondEquityPropsConfig::getEquityPropsId)
                .collect(Collectors.toList());
    }

    public Boolean getIsExistByEquityPropsIdAndStatus(Long equityPropsId, Boolean status) {
        LambdaQueryWrapper<SecondEquityPropsConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondEquityPropsConfig::getEquityPropsId, equityPropsId);
        wrapper.eq(SecondEquityPropsConfig::getStatus, status);
        return count(wrapper) > 0;
    }

    public SecondEquityPropsConfig getLastOneByEquityPropsId(Long equityPropsId) {
        LambdaQueryWrapper<SecondEquityPropsConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondEquityPropsConfig::getEquityPropsId, equityPropsId);
        wrapper.eq(SecondEquityPropsConfig::getStatus, Boolean.TRUE);
        wrapper.last("order by id desc");
        return getOne(wrapper);
    }

    public List<SimplePropsDTO> getPropsByPanelId(Long panelId) {
        return baseMapper.getPropsByPanelId(panelId);
    }
}